package com.eshop.utils;

import java.text.DecimalFormat;
import java.math.BigDecimal;

public class BigNum
{
    public static final String BIG_NUM_FMT_COMMA = "#,###,###,###,###,###,##0.00";
    public static final String BIG_NUM_FMT = "##################0.00";
    public static final String BIG_NUM_HUNDRED = "100";
    public static final int BIG_NUM_SCALE = 2;
    private static final int DEF_DIV_SCALE = 10;
    
    public static BigDecimal add(final double value1, final double value2) {
        final BigDecimal b1 = BigDecimal.valueOf(value1);
        final BigDecimal b2 = BigDecimal.valueOf(value2);
        return new BigDecimal(b1.add(b2).toString());
    }
    
    public static BigDecimal add(final Object value1, final Object value2) {
        final BigDecimal b1 = new BigDecimal(value1.toString());
        final BigDecimal b2 = new BigDecimal(value2.toString());
        return new BigDecimal(b1.add(b2).toString());
    }
    
    public static BigDecimal sub(final double value1, final double value2) {
        final BigDecimal b1 = BigDecimal.valueOf(value1);
        final BigDecimal b2 = BigDecimal.valueOf(value2);
        return new BigDecimal(b1.subtract(b2).toString());
    }
    
    public static BigDecimal sub(final Object value1, final Object value2) {
        final BigDecimal b1 = new BigDecimal(value1.toString());
        final BigDecimal b2 = new BigDecimal(value2.toString());
        return new BigDecimal(b1.subtract(b2).toString());
    }
    
    public static BigDecimal mul(final double value1, final double value2) {
        final BigDecimal b1 = BigDecimal.valueOf(value1);
        final BigDecimal b2 = BigDecimal.valueOf(value2);
        return new BigDecimal(b1.multiply(b2).toString());
    }
    
    public static BigDecimal mul(final Object value1, final Object value2) {
        final BigDecimal b1 = new BigDecimal(value1.toString());
        final BigDecimal b2 = new BigDecimal(value2.toString());
        return new BigDecimal(b1.multiply(b2).toString());
    }
    
    public static BigDecimal div(final double value1, final double value2) throws IllegalAccessException {
        return div(value1, value2, 10);
    }
    
    public static BigDecimal div(final String value1, final String value2) throws IllegalAccessException {
        return div(value1, value2, 10);
    }
    
    public static BigDecimal div(final double value1, final double value2, final int scale) throws IllegalAccessException {
        if (scale < 0) {
            throw new IllegalAccessException("精确度不能小于0");
        }
        final BigDecimal b1 = BigDecimal.valueOf(value1);
        final BigDecimal b2 = BigDecimal.valueOf(value2);
        return new BigDecimal(b1.divide(b2, scale, 4).toString());
    }
    
    public static BigDecimal div(final String value1, final String value2, final int scale) throws IllegalAccessException {
        if (scale < 0) {
            throw new IllegalAccessException("精确度不能小于0");
        }
        final BigDecimal b1 = new BigDecimal(value1);
        final BigDecimal b2 = new BigDecimal(value2);
        return b1.divide(b2, scale, 4);
    }
    
    public static BigDecimal round(final double v, final int scale) throws IllegalAccessException {
        return div(v, 1.0, scale);
    }
    
    public static BigDecimal round(final String v, final int scale) throws IllegalAccessException {
        return div(v, "1", scale);
    }
    
    public static boolean equalTo(final BigDecimal b1, final BigDecimal b2) {
        return b1 != null && b2 != null && 0 == b1.compareTo(b2);
    }
    
    public static BigDecimal penny2dollar(final String v) {
        BigDecimal s = new BigDecimal("0.00");
        try {
            s = div(v, "100", 2);
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return s;
    }
    
    public static BigDecimal dollar2penny(final String v) {
        return mul(v, "100");
    }
    
    public static String formatNumber(final String v) {
        return formatNumber(v, "#,###,###,###,###,###,##0.00");
    }
    
    public static String formatNumber(final String v, final String pattern) {
        return new DecimalFormat(pattern).format(new BigDecimal(v));
    }
    
    public static void main(final String[] args) {
        System.out.println(new BigDecimal("0.00").toString());
    }
}
